//
//  ThreeSixtyMap.m
//  Dolphin
//
//  Created by Matthew on 12-1-23.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "ThreeSixtyMap.h"

@implementation ThreeSixtyMap
@synthesize curindex;
@synthesize mapId;
@synthesize delegate = _delegate;
@synthesize useJudge;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        imageList = [[NSMutableArray alloc] init];
        
        curindex = 0;
        trigger_count = 0;
        
        threeLevel = FALSE;
        useJudge = TRUE;
    }
    return self;
}

-(void) initTheMap:(NSMutableArray*) inArr
          fileType:(NSString *) inFileType
              inId:(int) inMapId
{
    fileType = [[NSString alloc] initWithString:inFileType];
    
    _x = 0;
    _y = 0;
    _width = 1024;
    _height = 768;
    
    for (int i = 0; i< 3; i++) 
    {
        UIImageView *testImageView =  [[UIImageView alloc] initWithFrame:CGRectMake(_x,_y,_width,_height)];
        
        NSString *tempFileName = [[NSString alloc] initWithString:[inArr objectAtIndex:i]];
        
        NSString *strPath = [[NSBundle mainBundle] pathForResource:tempFileName ofType:inFileType];
        
        [testImageView setImage:[UIImage imageWithContentsOfFile:strPath]];
        
        [imageList addObject:testImageView];
        
        [self addSubview:testImageView];
        
        if (i>=1) 
        {
            UIImageView *previousImg = [imageList objectAtIndex:i-1];
            previousImg.hidden = TRUE;
        }

    }

    threeLevel = TRUE;
    imageNameList = inArr;

    float kTimerInternal = 0.3f;//0.075f;
    objQueMagTimer = [NSTimer scheduledTimerWithTimeInterval: kTimerInternal target:self selector:@selector(objectManageProc) userInfo:nil repeats:YES]; 
    trigger_count = 3;
    
    mapId = inMapId;
}

-(void) initTheMap2:(NSMutableArray*) inArr
           fileType:(NSString *) inFileType
               inId:(int) inMapId
                inW:(int) _W
                inH:(int) _H
{
    fileType = [[NSString alloc] initWithString:inFileType];
    
    _x = 0;
    _y = 0;
    _width = _W;
    _height = _H;
    
    for (int i = 0; i< 3; i++) 
    {
        UIImageView *testImageView =  [[UIImageView alloc] initWithFrame:CGRectMake(_x,_y,_width,_height)];
        
        NSString *tempFileName = [[NSString alloc] initWithString:[inArr objectAtIndex:i]];
        
        NSString *strPath = [[NSBundle mainBundle] pathForResource:tempFileName ofType:inFileType];
        
        [testImageView setImage:[UIImage imageWithContentsOfFile:strPath]];
        
        [imageList addObject:testImageView];
        
        [self addSubview:testImageView];
        
        if (i>=1) 
        {
            UIImageView *previousImg = [imageList objectAtIndex:i-1];
            previousImg.hidden = TRUE;
        }
        
    }
    
    imageNameList = inArr;
    
    float kTimerInternal = 0.3f;//0.075f;
    objQueMagTimer = [NSTimer scheduledTimerWithTimeInterval: kTimerInternal target:self selector:@selector(objectManageProc) userInfo:nil repeats:YES]; 
    trigger_count = 3;
    
    mapId = inMapId;
}

-(void) objectManageProc
{
    if (trigger_count < [imageNameList count]) 
    {
        if (threeLevel && [imageList count] >= 13 && useJudge) 
        {
                //nothing
        }
        else
        {
            [self closeImages];
        }
        
        UIImageView *testImageView =  [[UIImageView alloc] initWithFrame:CGRectMake(_x,_y,_width,_height)];
        
        NSString *tempFileName = [[NSString alloc] initWithString:[imageNameList  objectAtIndex:trigger_count]];
        NSString *strPath = [[NSBundle mainBundle] pathForResource:tempFileName ofType:fileType];
        
        [testImageView setImage:[UIImage imageWithContentsOfFile:strPath]];
        [imageList addObject:testImageView];
        
        [self addSubview:testImageView];
        
        if (threeLevel && [imageList count] >= 13 && useJudge) 
        {            
            [self closeImages];
            [self showImageAt:12];
        }

        
        //hide the previous one
        //UIImageView *previousImg = [imageList objectAtIndex:trigger_count-1];
        //previousImg.hidden = TRUE;
        trigger_count++;
    }
    else
    {
        if(objQueMagTimer)
        {
            [objQueMagTimer invalidate];
            objQueMagTimer = Nil;
        }
    }
}

-(void) closeImages
{
    for (int i = 0; i< [imageList count]; i++) 
    {
        UIImageView *tempImageView = [imageList objectAtIndex:i];
        tempImageView.hidden = TRUE;
    }
}

-(void) showImageAt:(int) index
{
    if (index>=0 && index< [imageList count]) 
    {
        [self closeImages];
        
        UIImageView *tempImageView = [imageList objectAtIndex:index];
        tempImageView.hidden = FALSE;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    isOnMove = TRUE;
    
    CGPoint startPoint = [[touches anyObject] locationInView:self];
	previous_x = startPoint.x;
    previous_y = startPoint.y;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    if (isOnMove) 
    {
        CGPoint movedPoint = [[touches anyObject] locationInView:self];
        
        if (abs(movedPoint.x-previous_x)>8) 
        {
            if (movedPoint.x>previous_x) 
            {
                [self galleryShift:-1];
            }
            else if(movedPoint.x<previous_x)
            {
                [self galleryShift:1];
            }
            
            previous_x = movedPoint.x;
        }
        else if (movedPoint.y-previous_y>50) 
        {
            previous_y = movedPoint.y;
            isOnMove = FALSE;
            
            [_delegate swipeUpWithMapId:mapId];
        }
        else if (movedPoint.y-previous_y<-50) 
        {
            previous_y = movedPoint.y;
            isOnMove = FALSE;
            
            [_delegate swipeDownWithMapId:mapId];
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    isOnMove = FALSE;
}

-(void) galleryShift:(int) type
{
    if ([imageList count]<=2) 
    {
        NSLog(@"return~~");
        return;
    }
    
    if (type>0) //shift to next
    {
        curindex = (curindex+1)%[imageList count];
    }
    else if(type < 0)//shift to previous
    {
        curindex = curindex-1;//
        
        if (curindex < 0) 
        {
            curindex = [imageList count] -1;
        }
    }
    
    NSLog(@"REACH DEEP%d image length%d type%d",curindex,[imageList count],type);
    [self showImageAt:curindex];
 
}

- (void)dealloc
{
    [super dealloc];
}

-(void) reset
{
    if(objQueMagTimer)
    {
        [objQueMagTimer invalidate];
        objQueMagTimer = Nil;
    }
    
    for (UIImageView*singleView in [self subviews]) 
    {
        if ([singleView isKindOfClass:[UIImageView class]])
        {
            [singleView removeFromSuperview];
            [singleView release];
        }
    }

    if (imageList != Nil) 
    {
        [imageList removeAllObjects];
        [imageList release];
    }
}

@end
